﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.ServiceModel.Web;
using LearningProject.DataContract;
using LearningProject.Interface;

namespace LearningProject.Service
{
        /// <summary>
        /// 员工服务
        /// </summary>
        public class EmployeesService : IEmployeesService
        {
                #region 实现服务契约
                
                static IList<Employee> employees = new List<Employee>
                {
                         new Employee{ Id = "001", Name="张三", Department="开发部", Grade = "G1"}, 
                         new Employee{ Id = "002", Name="梅西", Department="市场部", Grade = "G2"}, 
                         new Employee{ Id = "003", Name="关羽", Department="开发部", Grade = "G3"}, 
                         new Employee{ Id = "004", Name="肥罗", Department="财务部", Grade = "G4"}, 
                         new Employee{ Id = "005", Name="巴马", Department="保安部", Grade = "G5"}, 
                };

                public IEnumerable<Employee> GetAll()
                {
                        return employees;
                }

                public Employee Get(string id)
                {
                        var employee = employees.FirstOrDefault(e => e.Id == id);
                        if (employee == null)
                                WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.NotFound;
                        return employee;
                }

                public Employee Create(Employee employee)
                {
                        employees.Add(employee);
                        return employee;
                }

                public void Update(Employee employee)
                {
                        this.Delete(employee.Id);
                        employees.Add(employee);
                }

                public void Delete(string id)
                {
                        var employee = this.Get(id);
                        if (employee != null)
                        {
                                employees.Remove(employee);
                        }
                }

                #endregion

                public static void RunWcf()
                {
                        using (WebServiceHost host = new WebServiceHost(typeof(EmployeesService)))
                        {
                                host.Open();
                                Console.WriteLine("EmployeesService服务已开启......");
                                Console.ReadKey();
                        }
                }
        }
}
